var resourceEdit = {
	resourceTypeId: null,
	resourceId:null,
	userId:null,
	group:null,
	resourceTypeName:new Array()
};

$(function(){
	changeActiveItem('resourceList');
	initData();
	setData();
	savehandle();
});
/**
 * 提示信息处理
 */
function savehandle(){
	$('#alertdiv').hide();
	$('#alertdiv').savehandle({
		fieldMap:{},
		successMsg: '修改成功！',
		failMsg: '修改失败,请重试！',
		success: successCallBack 
	});
}
/**
 * 资源编辑成功跳转
 */
function successCallBack(){
	location.href = 'resourceList.jsp';
}

function initData(){
	var id = location.hash;
	var ids = id.substring(1, id.length).split(',');
	resourceEdit.resourceId = ids[0]; 
	resourceEdit.group = ids[1];
	if(!heading.resourceId){
		heading.resourceId = resourceEdit.resourceId;
		heading.group = resourceEdit.group;
		console.log(heading.resourceId,heading.group);
	}
	if(!resourceEdit.resourceId){
		resourceEdit.resourceId = heading.resourceId;
		resourceEdit.group = heading.group;
	}
	$('#resourceEdit #resourceType').html('');
}

/**
*获取初始数据
* 
*/
function setData(){
	
	var resourceId = resourceEdit.resourceId;
	$.getJSON(
		'/fw/resource/resource_getResourceById.action',
		{
			resourceId:resourceId
		},
		function(data){
			if(data && data.rows && data.rows.length){
				var rows = data.rows[0];
				if(rows.resourceTypeId){
					var html1='';
					resourceEdit.resourceTypeId=rows.resourceTypeId;
					html1 += '<option  value="'+rows.resourceTypeId+'" selected="selected">'+rows.resourceTypeName+'</option>';
					$('#resourceEdit #resourceType').append(html1);
				}else{
					$('#resourceEdit #resourceType').val('');
				}
				if(rows.url){
					$('#resourceEdit #url').val(rows.url);
				}else{
					$('#resourceEdit #url').val('');
				}
				if(rows.title){
					$('#resourceEdit #title').val(rows.title);
				}else{
					$('#resourceEdit #title').val('');
				}

				if(rows.userName){
					$('#resourceEdit #userName').val(rows.userName);
				}else{
					$('#resourceEdit #userName').val('');
				}
				
				if(rows.password){
					$('#resourceEdit #password').val(rows.password);
				}else{
					$('#resourceEdit #password').val('');
				}
			}
		}
	);
	$.getJSON(
		'/fw/resource/resourceType_findAll.action',
		function(data){
			
			if(data&&data.rows&&data.rows.length){
	  			var rows = data.rows;
	  			var len = rows.length;
	  			var html = '';
	  			for(var i = 0; i < len; i++){
	  				if(rows[i].resourceTypeId){
	  					resourceTypeId = rows[i].resourceTypeId;
	  					if(resourceEdit.resourceTypeId != rows[i].resourceTypeId)
	  						if(rows[i].resourceTypeName)
	  							html += '<option  value="'+rows[i].resourceTypeId+'">'+rows[i].resourceTypeName+'</option>';
	  					}
	  				
	  			}
	  			$('#resourceEdit #resourceType').append(html);
	  		}
		}
	);
}
/**
 * 下拉框内容改变时，重新请求数据
 * @param {Object} value
 */
function getChange(value){
	resourceTypeId = value;
}
/**
 * 提交修改信息
 * @return {TypeName} 
 */
function submitResource() {
	var resourceId = resourceEdit.resourceId;
	var userId = resourceEdit.userId;
	
	var resourceTypeId = $('#resourceEdit #resourceType').val();
	var resourceTypeName=resourceEdit.resourceTypeName[resourceTypeId];

	if(null == resourceTypeId || "" == resourceTypeId) {
		$('#alertdiv').savehandle('setmsg',{msg:'资源类型不能为空!',timer:2000});
		$('#resourceEdit #resourceType').focus();
		return false;
	}
	var url = $('#resourceEdit #url').val();
	if(null == url || "" == url) {
		$('#alertdiv').savehandle('setmsg',{msg:'资源链接不能为空!',timer:2000});
		$('#resourceEdit #url').focus();
		return false;
	}
	var title = $('#resourceEdit #title').val();
	if(null == title || "" == title) {
		$('#alertdiv').savehandle('setmsg',{msg:'资源名称不能为空!',timer:2000});
		$('#resourceEdit #title').focus();
		return false;
	}
 	$.getJSON(
		'/fw/resource/resource_editResource.action',
		{
			resourceId:resourceEdit.resourceId,
			'resourceType.resourceTypeId': resourceTypeId,
			url:url,
			title:title
		},
		function(data){
			$('#alertdiv').savehandle('handle',data,2000);
		}
	);
}

/**
 * 重置资源表单信息
 */
function resetData() {
	setData();
}